package com.pj.douban;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.Collections;
import java.util.HashMap;

/**
 * @author 王存露
 * @date 2021/2/17
 */
public class DoubanReptile {
    public static void main(String[] args) {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        try {
            for (int i = 0; i < 100; i = i + 20) {
                StringBuilder requestUrl = new StringBuilder(DoubanConst.DOU_BAN_TV_NEW);
                requestUrl.append(i);
                String body = HttpRequest.get(requestUrl.toString()).execute().body();
                if (JSONUtil.isJson(body)) {
                    JSONObject jsonObject = JSONUtil.parseObj(body);
                    if (jsonObject.containsKey("subjects")) {
                        JSONArray listUrl = jsonObject.getJSONArray("subjects");
                        for (Object alone : listUrl) {
                            JSONObject jsonInfo = (JSONObject) alone;
                            String url = jsonInfo.getStr("url");
                            String pageInfo = HttpRequest.get(url).execute().body();
                            String subString = "<span property=\"v:votes\">";
                            int i1 = pageInfo.indexOf(subString);
                            int i2 = pageInfo.indexOf("</span>人评价");
                            if (i1 < 0 || i2 < 0) {
                                continue;
                            }
                            String commentSum = pageInfo.substring(i1 + subString.length(), i2);
                            String title = jsonInfo.getStr("title");
                            if (!stringStringHashMap.containsKey(title)) {
                                System.out.println(title + "-" + commentSum);
                                stringStringHashMap.put(title, commentSum);
                            }
                            Thread.sleep(2000);
                        }
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
            System.out.println("异常推出");
            Collections.synchronizedMap()
        }
        System.out.println("一共找到" + stringStringHashMap.size() + "数据");
        stringStringHashMap.forEach((s, s2) -> {
            try {
                Integer integer = Integer.valueOf(s2);
                if (integer > 10000) {
                    System.out.println("符合条件的 title : " + s + " comments: " + integer);
                }
            } catch (NumberFormatException e) {
                System.out.println("处理异常数据 title : " + s + " comments: " + s2);
            }
        });
    }
}
